<script setup>
import request from '@/utils/request'

// 验证token并处理路由跳转
const verifyTokenAndRedirect = async () => {
  const token = uni.getStorageSync('token')
  if (!token) {
    uni.reLaunch({
      url: '/pages/login/index'
    })
    return
  }

  try {
    const { data } = await request({
      url: '/api/auth/verify',
      method: 'POST'
    })

    if (data.valid) {
      uni.reLaunch({
        url: '/pages/index/index'
      })
    } else {
      // token无效，清除存储并跳转到登录页
      uni.removeStorageSync('token')
      uni.removeStorageSync('userInfo')
      uni.reLaunch({
        url: '/pages/login/index'
      })
    }
  } catch (error) {
    console.error('Token验证失败:', error)
    uni.removeStorageSync('token')
    uni.removeStorageSync('userInfo')
    uni.reLaunch({
      url: '/pages/login/index'
    })
  }
}

// 应用生命周期
const app = {
  onLaunch() {
    verifyTokenAndRedirect()
  },
  onShow() {
    console.log('App Show')
  },
  onHide() {
    console.log('App Hide')
  }
}

app.onLaunch()</script>

<style>
	/*每个页面公共css */
</style>
